<?php
namespace app\mobile\controller;

use think\Db;
use think\Loader;
use think\Request;
use app\mobile\model\MemberOrder;

class Alipay
{
    /*
     * 支付宝支付
     */
    public function pay()
    {
        if (Request::instance()->isPost()) {
            header("Content-type: text/html; charset=utf-8");
            Loader::import('alipay.wappay.service.AlipayTradeService');
            Loader::import('alipay.wappay.buildermodel.AlipayTradeWapPayContentBuilder');
            Loader::import('alipay.config');
            global $config;
            $oid = Request::instance()->post('oid');
            if (!empty($oid)&& trim($oid)!=""){
                //商户订单号，商户网站订单系统中唯一订单号，必填
                // $out_trade_no = $oid;

                //订单名称，必填
                $subject = '三十元';

                //付款金额，必填
                //查询订单总价
                $totalArr = (new MemberOrder())->where(['status' => 0, 'id' => ['in' , $oid]  ])->field('sum(price) as total_price,sum(freight) as total_freight,order_number,shop_order_number')->find();
                $total_amount = bcadd($totalArr['total_price'],$totalArr['total_freight'],2);

                $out_trade_no = $totalArr['shop_order_number'];  //单店铺
                if (stristr($oid,',')){  //多店铺(购物车提交)
                    $out_trade_no = $totalArr['order_number'];
                }
                //商品描述，可空
                $body = Request::instance()->post('body','');

                //超时时间
                $timeout_express="1m";

                $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
                $payRequestBuilder->setBody($body);
                $payRequestBuilder->setSubject($subject);
                $payRequestBuilder->setOutTradeNo($out_trade_no);
                $payRequestBuilder->setTotalAmount($total_amount);
                $payRequestBuilder->setTimeExpress($timeout_express);

                $payResponse = new \AlipayTradeService($config);
                $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);

                return $result;
            }
        }

    }

    /**
     * 支付宝充值
     */
    public function rechage()
    {
        if (Request::instance()->isPost()) {

            header("Content-type: text/html; charset=utf-8");
            Loader::import('alipay.wappay.service.AlipayTradeService');
            Loader::import('alipay.wappay.buildermodel.AlipayTradeWapPayContentBuilder');
            Loader::import('alipay.config');

            global $config;

            //商户订单号，商户网站订单系统中唯一订单号，必填
            $oid = Request::instance()->param('order_number');

            if (!empty($oid)&& trim($oid)!=""){
                //商户订单号，商户网站订单系统中唯一订单号，必填
                // $out_trade_no = $oid;

                //订单名称，必填
                $subject = '高原狼商城支付';

                //查询订单总价
                $total_amount = Request::instance()->param('price');


                $out_trade_no = $oid;

                //商品描述，可空
                $body = Request::instance()->post('body','');

                //超时时间
                $timeout_express="1m";

                $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
                $payRequestBuilder->setBody($body);
                $payRequestBuilder->setSubject($subject);
                $payRequestBuilder->setOutTradeNo($out_trade_no);
                $payRequestBuilder->setTotalAmount($total_amount);
                $payRequestBuilder->setTimeExpress($timeout_express);

                $payResponse = new \AlipayTradeService($config);

                $config['return_url'] = request()->domain() . '/index.php/mobile//user_info/my_shop_index';
                $config['notify_url'] = request()->domain() . '/index.php/interfaces/Alipay/recharge';

                $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);

                return $result;
            }


        }
    }


    public function auction()
    {
        if (Request::instance()->isPost()) {

            header("Content-type: text/html; charset=utf-8");
            Loader::import('alipay.wappay.service.AlipayTradeService');
            Loader::import('alipay.wappay.buildermodel.AlipayTradeWapPayContentBuilder');
            Loader::import('alipay.config');

            global $config;

            //商户订单号，商户网站订单系统中唯一订单号，必填
            $oid = Request::instance()->param('oid');

            if (!empty($oid)&& trim($oid)!=""){
                //商户订单号，商户网站订单系统中唯一订单号，必填
                // $out_trade_no = $oid;

                //订单名称，必填
                $subject = '高原狼商城支付';
                $find = Db::name('auction_order')->where('id',$oid)->find();

                //查询订单总价
                $total_amount = $find['price'];
                $out_trade_no = $find['order_number'];

                //商品描述，可空
                $body = Request::instance()->post('body','');

                //超时时间
                $timeout_express="1m";

                $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
                $payRequestBuilder->setBody($body);
                $payRequestBuilder->setSubject($subject);
                $payRequestBuilder->setOutTradeNo($out_trade_no);
                $payRequestBuilder->setTotalAmount($total_amount);
                $payRequestBuilder->setTimeExpress($timeout_express);

                $payResponse = new \AlipayTradeService($config);

                $config['return_url'] = request()->domain() . '/index.php/mobile//user_info/order_list?type=1';
                $config['notify_url'] = request()->domain() . '/index.php/interfaces/Alipay/auction';

                $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);

                return $result;
            }


        }
    }

}
